/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_METADATA_TYPED_ID_HPP
#define _CUBEFS_CORE_API_METADATA_TYPED_ID_HPP

#include <cubefs/api/metadata/id.hpp>
#include <cubefs/core/api/metadata/id.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/metadata/typed_id.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename IDType, typename Interface>
class TypedIDFieldImplT: public IDFieldImplT<Interface> {
	typedef TypedIDFieldImplT<IDType, Interface>	Me;
	typedef IDFieldImplT<Interface> 				Base;
public:

	TypedIDFieldImplT(Int ptr, Int abi_ptr, StringRef name, Int offset, Int count):
		Base(ptr, abi_ptr, name, sizeof(IDType), offset, count) {}

	TypedIDFieldImplT(Int ptr, Int abi_ptr, StringRef name, Int count):
		Base(ptr, abi_ptr, name, sizeof(IDType), count) {}

	TypedIDFieldImplT(Int ptr, Int abi_ptr, StringRef name):
		Base(ptr, abi_ptr, name, sizeof(IDType), 1) {}


	virtual const IDValue GetIDValue(const void *mem) {
		if (mem == NULL) {
			throw NullPointerException("Pointer to ID can't be NULL");
		}

		const IDType* id = static_cast<const IDType*>(mem);

		return IDValue(id);
	}

	virtual void SetIDData(const IDValue& idValue, void* mem) {
		if (mem == NULL) {
			throw NullPointerException("Pointer to ID can't be NULL");
		}

		IDType* id = static_cast<IDType*>(mem);

		id->CopyFrom(idValue.ptr());
	}

//	static void Init() {
//	    AbstractType *type = new AbstractType(TypeNameFactory<Interface>::name(), IDField::MyType());
//	    type_ = type;
//	}
//
//    CUBEFS_DECLARE_REFLECTION;
};


//template <typename IDType, typename Interface>
//Type* TypedIDFieldImplT<IDType, Interface>::type_ 		= NULL;

template <typename IDType>
class TypedIDFieldImpl: public TypedIDFieldImplT<IDType, IDField> {
	typedef TypedIDFieldImplT<IDType, IDField> Base;

public:
	TypedIDFieldImpl(Int ptr, Int abi_ptr, StringRef name):
			Base(ptr, abi_ptr, name) {}

};



}}

#undef CUBEFS_SOURCE
#endif
